package com.bidv.wcm;

import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.Document;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.FileComponent;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.RichTextComponent;
import com.ibm.workplace.wcm.api.VirtualPortalScopedAction;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.exceptions.WCMException;
import com.ibm.workplace.wcm.api.query.Query;
import com.ibm.workplace.wcm.api.query.ResultIterator;

import com.ibm.workplace.wcm.api.query.Selectors;

public class VPContent implements VirtualPortalScopedAction{

	
	public String Data = "";
	public String requestID = "";
	public String doclib = "";
	//public constructor
	public VPContent(){
	}


	@Override
	public void run() throws WCMException {
		Repository repo = WCM_API.getRepository();
		Workspace myWorkspace = repo.getSystemWorkspace();
		myWorkspace.login();
		DocumentLibrary docLib = myWorkspace.getDocumentLibrary(doclib);
		myWorkspace.setCurrentDocumentLibrary(docLib);
		StringBuilder sb = new StringBuilder(); 
		try {
		    DocumentId<Document> documentId = myWorkspace.createDocumentId(requestID);
			Query myQuery = myWorkspace.getQueryService().createQuery();
			myQuery.addSelector(Selectors.idEquals(documentId));
			myWorkspace.getQueryService().execute(myQuery);
			ResultIterator<?> iterator =  myWorkspace.getQueryService().execute(myQuery);
			if(iterator.hasNext()) {
				Content content = (Content) iterator.next();
				String[] s = content.getComponentNames();
				for (int i=0;i<s.length;i++){
					System.out.println(s[i]);
				}
				sb.append("<h2>"+content.getTitle()+"</h2>");
				RichTextComponent  r1 = (RichTextComponent) content.getComponent("Summary");
				sb.append(r1.getRichText());
				RichTextComponent  r2 = (RichTextComponent) content.getComponent("Body");
				sb.append(r2.getRichText());
				//FileComponent r3 = (FileComponent) content.getComponent("Video");
			}
			Data = sb.toString();
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
}